package com.czxy.controller;

import com.czxy.domain.User;
import com.czxy.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;

    @RequestMapping("/findAll")
    public String findAll(Model model){
        //获取所有数据
        List<User> list = userService.findAll();
        // 使用model转存
        model.addAttribute("list", list);
        // 请求转发给list.jsp
        return "/user/list.jsp";
//        return "list";
    }


    @RequestMapping("/findByInfo")
    public  String findByInfo(User user,Model model){

        List<User> list = userService.findByInfo(user);

        model.addAttribute("list",list);
        model.addAttribute("user",user);

        return "/user/list.jsp";
    }


    @RequestMapping("/add")
    public  String add(User user ,Model model){

        //调用Service得到添加结果
        boolean rel = userService.add(user);
        //根据结果响应
        if (rel){
            //添加成功
            return "redirect:/user/findAll.action";

        }else{
            //添加失败
            model.addAttribute("message","添加失败,登录名重复");
            model.addAttribute("user",user);
            return "user/add.jsp";
        }
    }

































}
